<?php
/**
* @file $Id: Search.fnc.php 518 2007-05-14 07:27:34Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

function Search($type,$extra='')
{	global $_FOCUS;

	switch($type)
	{
		case 'student_id':
			if($_REQUEST['bottom_back'])
			{
				unset($_SESSION['student_id']);
				if(Preferences('MENU')=='Side')
					echo '<script language=JavaScript>parent.side.location="'.$_SESSION['Side_PHP_SELF'].'?modcat="+parent.side.document.forms[0].modcat.value;</script>';			
			}
			if($_SESSION['unset_student'])
			{
				unset($_REQUEST['student_id']);
				unset($_SESSION['unset_student']);
			}
			
			if($_REQUEST['LO_index'] && $_SESSION['student_list'][($_REQUEST['LO_index']-1)]['student_id']!=$_SESSION['student_id'] && $_REQUEST['student_id']!='new')
			{
				unset($_SESSION['staff_id']);
				$_SESSION['student_id'] = $_SESSION['student_list'][($_REQUEST['LO_index']-1)]['student_id'];
				$_SESSION['UserSchool'] = $_SESSION['student_list'][($_REQUEST['LO_index']-1)]['school_id'];
				if(!$_REQUEST['_FOCUS_PDF'] && Preferences('MENU')=='Side')
					echo '<script language=JavaScript>parent.side.location="'.$_SESSION['Side_PHP_SELF'].'?modcat="+parent.side.document.forms[0].modcat.value;</script>';			
			}
			elseif($_REQUEST['student_id'])
			{
				if($_REQUEST['student_id']!='new')
				{
					unset($_SESSION['staff_id']);
					$_SESSION['student_id'] = $_REQUEST['student_id'];
					if($_REQUEST['school_id'])
						$_SESSION['UserSchool'] = $_REQUEST['school_id'];
				}
				else
					unset($_SESSION['student_id']);

				if(!$_REQUEST['_FOCUS_PDF'] && Preferences('MENU')=='Side')
					echo '<script language=JavaScript>parent.side.location="'.$_SESSION['Side_PHP_SELF'].'?modcat="+parent.side.document.forms[0].modcat.value;</script>';			
			}
			
			if((!UserStudentID() && $_REQUEST['student_id']!='new') || $extra['new']==true)
			{
				$_REQUEST['next_modname'] = $_REQUEST['modname'];
				include('modules/Students/Search.php');
			}
		break;

		case 'staff_id':
			if(!$_REQUEST['staff_id'] && User('PROFILE')=='teacher')
				$_REQUEST['staff_id'] = User('STAFF_ID');
			
			if($_REQUEST['staff_id'])
			{
				if($_REQUEST['staff_id']!='new')
				{
					unset($_SESSION['student_id']);
					$_SESSION['staff_id'] = $_REQUEST['staff_id'];
					if($_REQUEST['school_id'])
						$_SESSION['UserSchool'] = $_REQUEST['school_id'];
				}
				else
					unset($_SESSION['staff_id']);
				if(!$_REQUEST['_FOCUS_PDF'] && Preferences('MENU')=='Side')
					echo '<script language=JavaScript>parent.side.location="'.$_SESSION['Side_PHP_SELF'].'?modcat="+parent.side.document.forms[0].modcat.value;</script>';			
			}
			elseif(!$_SESSION['staff_id'])
			{
				$_REQUEST['modfunc'] = 'search';
				$_REQUEST['next_modname'] = $_REQUEST['modname'];
				$_REQUEST['modname'] = 'Users/Search.php';
				include('modules/Users/Search.php');
			}
		break;

		case 'general_info':
			echo '<A onclick="switchMenu(\'general_table\');"><IMG SRC=assets/arrow_down.gif id=general_table_arrow> <B>'._('General').'</B></A><BR><TABLE class=EntireBox width=100% id=general_table>';
			echo '<tr><td align='.ALIGN_RIGHT.' width=150>'._('Last Name').'</td><td><input type=text name="last" size=30'.(($_REQUEST['bottom_back']=='true' && $_SESSION['Search_vars']['last'])?' value="'.$_SESSION['Search_vars']['last'].'"':'').'></td></tr>';
			echo '<tr><td align='.ALIGN_RIGHT.' width=150>'._('First Name').'</td><td><input type=text name="first" size=30'.(($_REQUEST['bottom_back']=='true' && $_SESSION['Search_vars']['first'])?' value="'.$_SESSION['Search_vars']['first'].'"':'').'></td></tr>';
			echo '<tr><td align='.ALIGN_RIGHT.' width=150>'._('Student ID').'</td><td><input type=text name="stuid" size=30'.(($_REQUEST['bottom_back']=='true' && $_SESSION['Search_vars']['stuid'])?' value="'.$_SESSION['Search_vars']['stuid'].'"':'').'></td></tr>';
			echo '<tr><td align='.ALIGN_RIGHT.' width=150>'._('Address').'</td><td><input type=text name="addr" size=30'.(($_REQUEST['bottom_back']=='true' && $_SESSION['Search_vars']['addr'])?' value="'.$_SESSION['Search_vars']['addr'].'"':'').'></td></tr>';
	
			$list = DBGet(DBQuery("SELECT DISTINCT TITLE,ID,SORT_ORDER FROM SCHOOL_GRADELEVELS WHERE SCHOOL_ID='".UserSchool()."' ORDER BY SORT_ORDER"));
			echo '<TR><TD align='.ALIGN_RIGHT.' width=150>'._('Grade').'</TD><TD><SELECT name=grade><OPTION value="">'._('N/A').'</OPTION>';
			if($_REQUEST['bottom_back']=='true' && $_SESSION['Search_vars']['grade'])
				$bb_grade = $_SESSION['Search_vars']['grade'];
			foreach($list as $value)
				echo "<OPTION value=$value[ID]".($bb_grade==$value['ID']?' SELECTED':'').">$value[TITLE]</OPTION>";
			echo '</SELECT></TD></TR>';

			$list = DBGet(DBQuery("SELECT DISTINCT TITLE,CALENDAR_ID FROM ATTENDANCE_CALENDARS WHERE SCHOOL_ID='".UserSchool()."' ORDER BY TITLE"));
			echo '<TR><TD align='.ALIGN_RIGHT.' width=150>'._('Calendar').'</TD><TD><SELECT name=calendar_id><OPTION value="">'._('N/A').'</OPTION>';
			if($_REQUEST['bottom_back']=='true' && $_SESSION['Search_vars']['calendar_id'])
				$bb_calendar = $_SESSION['Search_vars']['calendar_id'];
			foreach($list as $value)
				echo "<OPTION value=$value[CALENDAR_ID]".($bb_calendar==$value['CALENDAR_ID']?' SELECTED':'').">$value[TITLE]</OPTION>";
			echo '</SELECT></TD></TR>';
			echo '</TABLE>';

		break;

		case 'student_fields':
			echo '<BR><small> &nbsp; &nbsp;'._('Student Fields').'</small><BR>';
			$categories_RET = DBGet(DBQuery("SELECT ID,TITLE FROM STUDENT_FIELD_CATEGORIES ORDER BY SORT_ORDER,TITLE"));
			foreach($categories_RET as $category)
			{
				if(AllowUse('Students/Student.php&category_id='.$category['ID']))
				{
					$search_fields_RET = DBGet(DBQuery("SELECT 'CUSTOM_'||cf.ID AS COLUMN_NAME,cf.TYPE,cf.TITLE,cf.SELECT_OPTIONS FROM CUSTOM_FIELDS cf WHERE CATEGORY_ID='".$category['ID']."'"),array(),array('TYPE'));
					if(!count($search_fields_RET))
						continue;

					echo '<A onclick="switchMenu(\'fields_'.$category['ID'].'_table\');"><IMG SRC=assets/arrow_right.gif id=fields_'.$category['ID'].'_table_arrow> <B>'.$category['TITLE'].'</B></A><BR><DIV width=100% id=fields_'.$category['ID'].'_table style="display:none;">';
					echo '<TABLE width=100% class=EntireBox>';
				
					if(count($search_fields_RET['text']))
					{
						foreach($search_fields_RET['text'] as $column)
							echo "<TR><TD align=".ALIGN_RIGHT." width=150><font color=black>$column[TITLE]</font></TD><TD><INPUT type=text name=cust[{$column[COLUMN_NAME]}] size=30".(($_REQUEST['bottom_back']=='true' && $_SESSION['Search_vars']['cust'][$column['COLUMN_NAME']])?' value="'.$_SESSION['Search_vars']['cust'][$column['COLUMN_NAME']].'"':'')."></TD></TR>";
					}
					if(count($search_fields_RET['numeric']))
					{
						foreach($search_fields_RET['numeric'] as $column)
							echo "<TR><TD align=".ALIGN_RIGHT." width=150>$column[TITLE]</TD><TD><small>Between</small> <INPUT type=text name=cust_begin[{$column[COLUMN_NAME]}] size=3 maxlength=11".(($_REQUEST['bottom_back']=='true' && $_SESSION['Search_vars']['cust'][$column['COLUMN_NAME']])?' value="'.$_SESSION['Search_vars']['cust_begin'][$column['COLUMN_NAME']].'"':'')."> <small>&amp;</small> <INPUT type=text name=cust_end[{$column[COLUMN_NAME]}] size=3 maxlength=11".(($_REQUEST['bottom_back']=='true' && $_SESSION['Search_vars']['cust'][$column['COLUMN_NAME']])?' value="'.$_SESSION['Search_vars']['cust_end'][$column['COLUMN_NAME']].'"':'')."></TD></TR>";
					}
					//echo '</TABLE><TABLE>';
					if(count($search_fields_RET['select']))
					{
						foreach($search_fields_RET['select'] as $column)
						{					
							$column['SELECT_OPTIONS'] = str_replace("\n","\r",str_replace("\r\n","\r",$column['SELECT_OPTIONS']));
							$options = explode("\r",$column['SELECT_OPTIONS']);
							
							echo "<TR><TD align=".ALIGN_RIGHT." width=150><font color=black>$column[TITLE]</font></TD><TD>";
							if($_REQUEST['bottom_back']=='true' && $_SESSION['Search_vars']['cust'][$column['COLUMN_NAME']])
								$bb_option = $_SESSION['Search_vars']['cust'][$column['COLUMN_NAME']];
							else
								$bb_option = '';
							echo "<SELECT name=cust[{$column[COLUMN_NAME]}] style='max-width:250;'><OPTION value=''>"._('N/A')."</OPTION><OPTION value='!'".($bb_option=='!'?' SELECTED':'').">No Value</OPTION>";
							foreach($options as $option)
								echo "<OPTION value=\"$option\"".(($column['COLUMN_NAME']=='CUSTOM_44' && $column['TITLE']=='District' && $option==$_SESSION['district'])?' SELECTED':'').($bb_option==$option?' SELECTED':'').">$option</OPTION>";
							echo '</SELECT>';
							echo "</TD></TR>";
						}
					}
					if(count($search_fields_RET['multiple']))
					{
						foreach($search_fields_RET['multiple'] as $column)
						{					
							$column['SELECT_OPTIONS'] = str_replace("\n","\r",str_replace("\r\n","\r",$column['SELECT_OPTIONS']));
							$options = explode("\r",$column['SELECT_OPTIONS']);
							
							echo "<TR><TD align=".ALIGN_RIGHT." width=150><font color=black>$column[TITLE]</font></TD><TD>";
							if($_REQUEST['bottom_back']=='true' && $_SESSION['Search_vars']['cust'][$column['COLUMN_NAME']])
								$bb_option = $_SESSION['Search_vars']['cust'][$column['COLUMN_NAME']];
							else
								$bb_option = '';
							echo "<SELECT name=cust[{$column[COLUMN_NAME]}] style='max-width:250;'><OPTION value=''>"._('N/A')."</OPTION><OPTION value='!'".($bb_option=='!'?' SELECTED':'').">"._('No Value')."</OPTION>";
							foreach($options as $option)
								echo "<OPTION value=\"$option\"".($bb_option==$option?' SELECTED':'').">$option</OPTION>";
							echo '</SELECT>';
							echo "</TD></TR>";
						}
					}			//echo '</TABLE><TABLE>';
					if(count($search_fields_RET['date']))
					{
						foreach($search_fields_RET['date'] as $column)
						{
							if($_REQUEST['bottom_back']=='true' && $_SESSION['Search_vars']['cust'][$column['COLUMN_NAME']])
								$bb_option = $_SESSION['Search_vars']['cust'][$column['COLUMN_NAME']];
							else
								$bb_option = '';
							echo "<TR><TD colspan=2><font color=black>$column[TITLE]<BR> &nbsp; &nbsp; <small>"._('Between')."</small> ".PrepareDate($bb_option,'_cust_begin['.$column['COLUMN_NAME'].']',true,array('short'=>true)).' <small>&</small> '.PrepareDate('','_cust_end['.$column['COLUMN_NAME'].']',true,array('short'=>true))."</font></TD></TR>";
						}
					}
					if(count($search_fields_RET['log']))
					{
						$log_RET = DBGet(DBQuery("SELECT 'CUSTOM_'||ID as ID,LOG_NUMERIC,LOG_DATE,LOG_SELECT,SELECT_OPTIONS,LOG_NUMERIC_TITLE,LOG_DATE_TITLE,LOG_SELECT_TITLE FROM CUSTOM_FIELDS WHERE TYPE='log'"),array(),array('ID'));
						foreach($search_fields_RET['log'] as $column)
						{
							echo "<TR><TD colspan=2><font color=black>$column[TITLE]";
							if($log_RET[$column['COLUMN_NAME']][1]['LOG_SELECT'] || $log_RET[$column['COLUMN_NAME']][1]['LOG_NUMERIC'])
								echo '<BR> &nbsp; &nbsp; <TABLE style="display:inline;"><TR>';
							if($log_RET[$column['COLUMN_NAME']][1]['LOG_SELECT'])
							{
								$id = substr($column['COLUMN_NAME'],7);
								$column['SELECT_OPTIONS'] = str_replace("\n","\r",str_replace("\r\n","\r",$column['SELECT_OPTIONS']));
								$options = explode("\r",$column['SELECT_OPTIONS']);						
								
								echo "<TD><font color=black>".$log_RET[$column['COLUMN_NAME']][1]['LOG_SELECT_TITLE']."</font></TD>";
								if($_REQUEST['bottom_back']=='true' && $_SESSION['Search_vars']['cust_log'][$id][$column['COLUMN_NAME']])
									$bb_option = $_SESSION['Search_vars']['cust_log'][$id][$column['COLUMN_NAME']];
								else
									$bb_option = '';
								echo "<TD><SELECT name=cust_log[{$column[COLUMN_NAME]}][LOG_SELECT] style='max-width:250;'><OPTION value=''>"._('N/A')."</OPTION><OPTION value='!'".($bb_option=='!'?' SELECTED':'').">"._('No Value')."</OPTION>";
								foreach($options as $option)
									echo "<OPTION value=\"$option\"".($bb_option==$option?' SELECTED':'').">$option</OPTION>";
								echo '</SELECT></TD>';
							}
							if($log_RET[$column['COLUMN_NAME']][1]['LOG_NUMERIC'])
								echo "<TD>".$log_RET[$column['COLUMN_NAME']][1]['LOG_NUMERIC_TITLE']."</TD><TD><small>Between</small> <INPUT type=text name=cust_log[{$column[COLUMN_NAME]}][LOG_NUMERIC][begin] size=3 maxlength=11".(($_REQUEST['bottom_back']=='true' && $_SESSION['Search_vars']['cust_log'][$column['COLUMN_NAME']]['LOG_NUMERIC']['begin'])?' value="'.$_SESSION['Search_vars']['cust_log'][$column['COLUMN_NAME']]['LOG_NUMERIC']['begin'].'"':'')."> <small>&amp;</small> <INPUT type=text name=cust_log[{$column[COLUMN_NAME]}][LOG_NUMERIC][end] size=3 maxlength=11".(($_REQUEST['bottom_back']=='true' && $_SESSION['Search_vars']['cust_log'][$column['COLUMN_NAME']]['LOG_NUMERIC']['end'])?' value="'.$_SESSION['Search_vars']['cust_log'][$column['COLUMN_NAME']]['LOG_NUMERIC']['end'].'"':'')."></TD>";
							if($log_RET[$column['COLUMN_NAME']][1]['LOG_SELECT'] || $log_RET[$column['COLUMN_NAME']][1]['LOG_NUMERIC'])
								echo '</TR></TABLE>';
							if($log_RET[$column['COLUMN_NAME']][1]['LOG_DATE'])
							{
								if($_REQUEST['bottom_back']=='true' && $_SESSION['Search_vars']['cust_log'][$column['COLUMN_NAME']])
									$bb_option = $_SESSION['Search_vars']['cust_log'][$column['COLUMN_NAME']];
								else
									$bb_option = '';
								echo "<BR> &nbsp; &nbsp; <small>"._('Between')."</small> ".PrepareDate($bb_option,'_cust_log['.$column['COLUMN_NAME'].'][LOG_DATE][begin]',true,array('short'=>true)).' <small>&</small> '.PrepareDate('','_cust_log['.$column['COLUMN_NAME'].'][LOG_DATE][end]',true,array('short'=>true))."</font></TD></TR>";
							}
						}
					}
					if(count($search_fields_RET['radio']))
					{
						echo '<TR><TD colspan=2><BR></TD></TR>';		
						echo "<TR><TD colspan=2><TABLE>";
					
						echo "<TR><TD></TD><TD><table border=0 cellpadding=0 cellspacing=0><tr><td width=25><b><font color=black>"._('All')."</font></b></td><td width=30><b><font color=black>"._('Yes')."</font></b></td><td width=25><b><font color=black>"._('No')."</font></b></td></tr></table></font></b></TD><TD></TD><TD></TD><TD>";
						if(count($search_fields_RET['radio'])>1)
							echo "<table border=0 cellpadding=0 cellspacing=0><tr><td width=25><b><font color=black>"._('All')."</font></b></td><td width=30><b><font color=black>"._('Yes')."</font></b></td><td width=25><b><font color=black>"._('No')."</font></b></td></tr></table>";
						echo "</TD></TR>";
		
						$side = 1;
						foreach($search_fields_RET['radio'] as $cust)
						{
							if($side%2!=0)
								echo '<TR>';
							echo "<TD align=".ALIGN_RIGHT."><font color=black>$cust[TITLE]</font></TD><TD>
								<table border=0 cellpadding=0 cellspacing=0><tr><td width=25 align=center>
								<input name='cust[{$cust[COLUMN_NAME]}]' type='radio' value=''".(($_REQUEST['bottom_back']=='true' && $_SESSION['Search_vars']['cust'][$column['COLUMN_NAME']])?'':' CHECKED')." />
								</td><td width=30 align=center>
								<input name='cust[{$cust[COLUMN_NAME]}]' type='radio' value='Y'".(($_REQUEST['bottom_back']=='true' && $_SESSION['Search_vars']['cust'][$column['COLUMN_NAME']]=='Y')?' CHECKED':'')." />
								</td><td width=25 align=center>
								<input name='cust[{$cust[COLUMN_NAME]}]' type='radio' value='N'".(($_REQUEST['bottom_back']=='true' && $_SESSION['Search_vars']['cust'][$column['COLUMN_NAME']])?' CHECKED':'')." />
								</td></tr></table>
								</TD><TD>&nbsp; &nbsp; &nbsp; &nbsp;</TD>";
							if($side%2==0)
								echo '</TR>';	
							$side++;
						}
						echo "</TABLE></TD></TR>";
					}
					echo '</TABLE>';
					echo '</DIV>';
				}
			}
		break;
	}
}
?>